package com.lizhivscaomei.txt2json;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class AppMain {
    public static void main(String[] args) throws IOException {
        Article article = new Article();
        // 文件流甚是牛逼
        Stream<String> stream = Files.lines(Paths.get("src/main/resources/raw.txt"));
        BufferedWriter writer = Files.newBufferedWriter(Paths.get("src/main/resources/raw.json"));
        writer.write("[\n");
        writer.flush();

        stream.forEach(msg -> {
            try {
                if (StringUtils.isNotEmpty(msg)) {
                    if (msg.contains("产品特点")) {
                        article.setIndex(1);
                    } else if (msg.contains("产品介绍")) {
                        article.setIndex(2);
                    } else if (msg.contains("建议零售价")) {
                        article.setPrice(msg);
                        writer.write(JSON.toJSONString(article) + ",\n");
                        writer.flush();
                        article.setIndex(0);
                    } else if (article.getIndex() == 0) {
                        article.setName(msg);
                    } else if (article.getIndex() == 1) {
                        article.setTedian(article.getTedian()+msg);
                    } else if (article.getIndex() == 2) {
                        article.setIntroduce(article.getIntroduce()+msg);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        writer.write("]\n");
        writer.flush();

    }

}
